/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.component.schema;

import com.inspur.edp.caf.cef.schema.datatype.DataType;
import com.inspur.edp.component.schema.enums.CollectionType;
import com.inspur.edp.component.schema.enums.GspCompDataType;

import java.util.List;

/**
 * @auther zhouyjtr
 * @date 2022年01月06日
 */
public final class GspCompParamType implements DataType {

    public static final String KIND = "CompDataType";

    //region Field
    private CollectionType collectionType = CollectionType.None;

    private GspCompDataType type;

    private String customClassName;

    private String customPkgName;

    private StructuredTypeRefer complexType;

    private GspCompParamType keyType;

    private GspCompParamType valueType;

    //endregion

    //region Prop
    public GspCompDataType getType() {
        return type;
    }

    public void setType(GspCompDataType type) {
        this.type = type;
    }

    public CollectionType getCollectionType() {
        return collectionType;
    }

    public void setCollectionType(CollectionType collectionType) {
        this.collectionType = collectionType;
    }

    public String getCustomClassName() {
        return customClassName;
    }

    public void setCustomClassName(String customClassName) {
        this.customClassName = customClassName;
    }

    public String getCustomPkgName() {
        return customPkgName;
    }

    public void setCustomPkgName(String customPkgName) {
        this.customPkgName = customPkgName;
    }

    public StructuredTypeRefer getComplexType() {
        return complexType;
    }

    public void setComplexType(StructuredTypeRefer complexType) {
        this.complexType = complexType;
    }

    public GspCompParamType getKeyType() {
        return keyType;
    }

    public void setKeyType(GspCompParamType keyType) {
        this.keyType = keyType;
    }

    public GspCompParamType getValueType() {
        return valueType;
    }

    public void setValueType(GspCompParamType valueType) {
        this.valueType = valueType;
    }

    //endregion

    //检验该类数据定义是否合理
    public boolean isValid() {
        switch (collectionType) {
            case None:
                return typeCheck();
            case List:
            case Array:
                return valueType != null && valueType.isValid();
            case Map:
                boolean valueCheck = valueType != null && valueType.isValid();
                boolean keyCheck = keyType != null && keyType.isValid();
                return valueCheck && keyCheck;
            default:
                return false;
        }
    }

    private boolean typeCheck() {
        if (type == null) {
            return false;
        }
        switch (type) {
            case Complex:
                return complexType != null;
            case Custom:
                return customClassName != null && customPkgName != null;
            default:
                return true;
        }
    }

    @Override
    public String getKind() {
        return KIND;
    }

    @Override
    @Deprecated
    public List<String> getExtensionKeys() {
        return null;
    }

    @Override
    @Deprecated
    public String getExtensionValue(String s) {
        return null;
    }
}
